#include "playbackvolumebutton.h"
#include <QDebug>
#include <QStylePainter>
#include <QStyleOptionButton>
#include <QMouseEvent>
#include <QAction>

PlaybackVolumeButton::PlaybackVolumeButton(QWidget *parent)
    : QPushButton(parent)
    , volumeSliderDlg(nullptr)
{
    this->setFixedSize(32,32);

    setStyleSheet("QPushButton{background-image:url(:/resources/audio_open.svg);border:none;}"
        "QPushButton:hover{background-image:url(:/resources/audio_open_hover.svg);border:none;}"
        "QPushButton:pressed{background-image:url(:/resources/audio_open.svg);border:none;}");
    connect(this, &QPushButton::clicked, this, &PlaybackVolumeButton::clickSlot);


    /*-----F1关联静音-----*/
    QAction *actionF1 = new QAction(this);
    actionF1->setShortcut(Qt::Key_F1);          // 绑定 F1
    connect(actionF1, &QAction::triggered,      // 连接信号
            this, &PlaybackVolumeButton::clickSlot);
    this->addAction(actionF1);
}

PlaybackVolumeButton::~PlaybackVolumeButton()
{}

//void PlaybackVolumeButton::setVLC(VLCKits *vlcKits)
//{
//    if(vlcKits){
//        _vlcKits = vlcKits;
//    }
//}

void PlaybackVolumeButton::enterEvent(QEvent *event)
{
    (void)event;
    qDebug() << "PlaybackVolumeButton::enterEvent()";
    if (!volumeSliderDlg)
        volumeSliderDlg = new PlaybackVolumeDialog(this);

    QPoint p1 = this->mapToGlobal(QPoint(0, 0));  //声音按钮左上角相对于屏幕的绝对位置
    QRect rect1 = this->rect();
    QRect rect2 = volumeSliderDlg->rect();     //rect包含标题栏，去掉标题栏后height不变

    int x = p1.x() +(rect1.width() - rect2.width()) / 2;
    int y = p1.y() - rect2.height();
    volumeSliderDlg->move(x, y);   //move是相对于桌面原点的位置
    volumeSliderDlg->show();

    if(isconnect){
        //connect(volumeSliderDlg, &PlaybackVolumeDialog::sigVolumeSliderPos, _vlcKits, &VLCKits::setVolume);
        connect(volumeSliderDlg, &PlaybackVolumeDialog::sigVolumeSliderPos, [=](int val){
            if(isMute && val != 0){
                //qDebug() << __LINE__;
                isMute = false;
                setStyleSheet(
               "QPushButton{background-image:url(:/resources/audio_open.svg);border:none;}"
               "QPushButton:hover{background-image:url(:/resources/audio_open_hover.svg);border:none;}"
               "QPushButton:pressed{background-image:url(:/resources/audio_open.svg);border:none;}");
            }
        });
        isconnect = false;
    }
    //一旦音量控制界面出现后，就开启定时器
    if(isRunning == false) {
        timerId = startTimer(250);//开启定时器,250毫秒的时间间隔
        isRunning = true;//定时器启动标志位
    }
}

void PlaybackVolumeButton::paintEvent(QPaintEvent*)
{
    //qDebug() << "PlaybackVolumeButton::paintEvent";
    QStylePainter p(this);
    QStyleOptionButton option;
    initStyleOption(&option);
    p.drawControl(QStyle::CE_PushButton, option);
}

void PlaybackVolumeButton::timerEvent(QTimerEvent *)
{
    //qDebug() << "PlaybackVolumeButton::timerEvent";
    hide();
}

void PlaybackVolumeButton::hide()
{
    if(volumeSliderDlg !=nullptr && volumeSliderDlg->isVisible()) {

        QPoint mousePos = QCursor::pos();//获取鼠标的位置
        QRect rect1 = volumeSliderDlg->geometry();
        QRect rect2 = this->geometry();
        QRect rect3(rect1.x(), rect1.y(), rect1.width(), rect1.height() + rect2.height());
        //根据鼠标的位置判断音量大小窗口是否消失
        if(!rect3.contains(mousePos)) {
            volumeSliderDlg->hide();
            //qDebug() << ">> hide...";
        }
    } else {
        killTimer(timerId);//当不包含鼠标位置，并且音量控制界面没显示时，就停止定时器
        isRunning = false;
    }
}

void PlaybackVolumeButton::clickSlot()
{
    isMute = !isMute;
    if(isMute) {//设置为静音
        if(volumeSliderDlg) {
            //保存原来的音量值
            volumeValue = volumeSliderDlg->value();
            volumeSliderDlg->setSliderValue(0);
            setStyleSheet("QPushButton{background-image:url(:/resources/audio_mute.png)}");
        }
    } else {
        if(volumeSliderDlg) {
            volumeSliderDlg->setSliderValue(volumeValue);
            setStyleSheet(
           "QPushButton{background-image:url(:/resources/audio_open.svg);border:none;}"
           "QPushButton:hover{background-image:url(:/resources/audio_open_hover.svg);border:none;}"
           "QPushButton:pressed{background-image:url(:/resources/audio_open.svg);border:none;}");
        }
    }
}
